package com.wsh.archive.service.businessImpl;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.wsh.archive.exception.ArchiveException;
import com.wsh.archive.service.business.ArchiveService;
import com.wsh.archive.vo.ArchiveTxtVO;

public class ArchiveTxtImpl implements ArchiveService<ArchiveTxtVO> {

	/**
	 * {@inheritDoc}
	 */
	public Map<Integer, String> read(ArchiveTxtVO vo) throws ArchiveException {
		
		final Map<Integer, String> result = new HashMap<Integer, String>();
		
		try {
		
			if (null == vo || vo.getFile() == null)
				throw new ArchiveException("INFORMAR O ARQUIVO NO 'ArchiveVO'.");
						
			BufferedReader reader = new BufferedReader(new FileReader(vo.getFile()));
			String line = null;
			int contador = 0;
			
			while ((line = reader.readLine()) != null) {
				contador ++;								
				result.put(contador, line);
			}
			
		} catch (FileNotFoundException e) {
			throw new ArchiveException("O ARQUIVO NAO FOI ENCONTRADO NO DIRETORIO ESPECIFICADO. ", e);
		} catch (IOException e) {
			throw new ArchiveException("ERRO AO LER O ARQUIVO. ", e);
		}		
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean export(ArchiveTxtVO vo) throws ArchiveException {
		// TODO Auto-generated method stub
		return false;
	}	
	
}